package de.dopichaj.labrador.search;


import java.io.IOException;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.lang.Validate;

import de.dopichaj.labrador.index.IndexVersionException;
import de.dopichaj.labrador.search.hit.DocHitMap;
import de.dopichaj.labrador.search.hit.Hit;
import de.dopichaj.labrador.search.hit.HitComparator;
import de.dopichaj.labrador.search.prune.ResultTreePruner;


/**
 * Serves as a wrapper class for easy access to all functionality.
 * 
 * @author Benedikt
 */
public class XMLSearcher {

    private final NEXISearcher searchProcessor;
    private final XMLSearchPostProcessor postProcessor;
    
    /**
     * Constructor for XMLSearcher.
     * @param pruner          The pruner that should be used for pruning.
     * @param erg             The ElementRelationshipGraph to use.
     * @param indexDirectory  Location of the de.dopichaj.labrador.index.
     * @throws IndexVersionException 
     */
    public XMLSearcher(final NEXISearcher searcher,
        final ResultTreePruner pruner) throws IOException {
        
        Validate.notNull(pruner);
        Validate.notNull(searcher);
        
        searchProcessor = searcher;
        postProcessor = new XMLSearchPostProcessor(pruner);
    }
    
    public void close() {
        searchProcessor.close();
    }

    /**
     * Applies searching with the results sorted with the
     * specified sort order.
     * @param comparator
     *      The comparator to use for sorting
     * @return Search results as a set of hits sorted by the given sort order
     * @throws SearchException 
     * @throws QueryParseException 
     */
    public List<Hit> search(String nexi, Comparator<Hit> comparator)
        throws SearchException, QueryParseException {
        
        Validate.notNull(nexi);
        
        DocHitMap results = searchProcessor.nexiSearch(nexi);
        postProcessor.prune(results);
        return results.getSortedHits(comparator);
    }

    /**
     * Applies searching with the results sorted by score.
     * @return Search results as a set of hits sorted by score
     * @throws SearchException 
     * @throws QueryParseException 
     */
    public List<Hit> search(String nexi) throws SearchException, QueryParseException {
        return search(nexi, new HitComparator(HitComparator.SORT_BY_SCORE));
    }

}
/*
Copyright (c) 2005 Benedikt Eger
Copyright (c) 2005-2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/